﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace ktds_test_module
{
    class DBConnect
    {
        public void Connect_DB()
        {
            SqlConnection conn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            
            //DB 연결정보
            try
            {
                String conStr = @"Data Souce=127.0.0.1;Database=DB_Name;UserId=user_id;Password=Pass_Word";

                conn.ConnectionString = conStr;

                //실행 쿼리문
                String query = "SELECT USERID FROM USER_TABLE WHERE LOGINID='abcde'";

                //쿼리 세팅
                cmd.CommandText = query;
                cmd.Connection = conn;

                //DB 오픈
                conn.Open();

                //쿼리 실행결과 담기 - SqlDataReader 또는 SqlDataAdapter를 사용하면 된다.
                SqlDataReader reader = cmd.ExecuteReader();
                String userId = "";

                //결과값 출력하기
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        userId = reader[0].ToString();
                    }
                }

                conn.Close();
            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.ToString());
            }

            finally
            {
                cmd.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }
    }
}
